package com.example.vote.springboot_vote.conf;

import com.example.vote.springboot_vote.bean.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.*;

import javax.servlet.MultipartConfigElement;

@Configuration
public class Conf implements WebMvcConfigurer {
    /**
     * #静态资源对外暴露的访问路径(访问图片的路径)
     * file.staticAccessPath=/upload/**
     */
    @Value("${file.staticAccessPath}")
    private String staticAccessPath;
    /**
     * # 设备截图文件保存路径(文件存在具体的文件夹的路径)
     * smas.captrue.image.path=home/capture/
     */
    @Value("${smas.captrue.image.path}")
    private String captureImagePath;
    /**
     * #文件上传后存储路径：D:\home\capture\5\*.jpg(具体配置如下)
     * #访问Url:http://ip:port/smas/upload/*
     * #文件上传目录(window是d/e/f盘， linux是/)（注意Linux和Windows上的目录结构不同）
     * #file.uploadFolder=/
     * file.uploadFolder=d://
     */
    @Value("${file.uploadFolder}")
    private String uploadFolder;

    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setLocation(uploadFolder);
        return factory.createMultipartConfig();
    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*").allowCredentials(true).allowedMethods("*").maxAge(3600);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(staticAccessPath).addResourceLocations("file:" + uploadFolder + captureImagePath);
    }



}
